package com.example.spring_aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/23
 * Time: 17:31
 */
@Slf4j
@Component
@Aspect
public class TImeRecordAspect {
//    @Around("execution(* com.example.spring_aop.controller.*.*(..))")
    @Around("@annotation(com.example.spring_aop.aspect.TimeRecord)")
//    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object timeRecord(ProceedingJoinPoint pjt) {
        // 1. 记录开始时间
        long start = System.currentTimeMillis();

        // 2. 执行目标代码
        Object o = null;
        try {
            o = pjt.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        // 3. 记录结束时间
        long end = System.currentTimeMillis();
        // 4. 返回结果
        log.info(pjt.getSignature() + "耗时" + (end - start) + "ms");
        return o;
    }
}
